var pageCount = 1;
function classOnload(a){
    console.log(a);

var Json={
    pageSize:9,
    pageIndex:a,
    fileType:0//0预备1在习
}
ajax.post(ajax.myUrl+"stu/studentList",Json,function(result){
    console.log(result);
    response(result);
    console.log(result.dataset.studentList);
    var data = result.dataset.studentList;
    var tbody = document.querySelector("tbody");
    for(var i=0;i<data.length;i++){
        var newtr = document.createElement("tr");
        newtr.innerHTML=`
        <td>${data[i].fileName}</td>
        <td>${data[i].fileSex}</td>
        <td>${data[i].clazzName}</td>
        <td>${data[i].fileDate}</td>
        <td>${data[i].filePhone}</td>
        <td><div class="remove">修改</div>
        <div class="delete">删除</div></td>
        `;
        tbody.appendChild(newtr)
        
    }
    //点击修改，弹出模态框
    //获取模态框
    var addpreStudent1 = document.getElementsByClassName("addpreStudent1")[0];
    var remove = document.getElementsByClassName("remove");
    for(var i=0;i<remove.length;i++){
        localStorage.setItem("Id","");
        remove[i].onclick=function(){
            for(var j=0;j<remove.length;j++){
                if(this==remove[j]){
                    // console.log(remove[j],j)
                    addpreStudent1.style.display = "block";
                    localStorage.setItem("Id",data[j].fileId);
                }
            }
        }
    }
    //点击修改用户模态框中的确定或取消，模态框消失
    var sure1 = addpreStudent1.getElementsByClassName("sure1")[0];
    sure1.onclick = function(){
        //生成学生账户
        var date = new Date();
        var month = date.getMonth();
        if(month<9){
            month = "0"+(month+1);  //月
        }
        var day = date.getDate();
        var num = Math.floor(Math.random()*100);
        if(num<10){
            num = "0"+num;  //月
        }
        var  StuAccount = "br"+month+day+num;
        //获取班级
        var select = document.querySelectorAll("select");
        // console.log(select[1].value)
        var classId = null;
        if(select[0].value=="JAVA班"){
            classId=1;
        }else if(select[0].value=="ui"){
            classId=2;
        }else if(select[0].value=="python"){
            classId=4;
        }
        // console.log(select[0])
        //获取姓名
        var inputs = document.querySelectorAll("input");
        // console.log(inputs)
        addpreStudent1.style.display = "none";
        // console.log(Number(inputs[7].value))
        var Json2 = {
            fileId:localStorage.getItem("Id"),//学生档案id
            fileType:0,
            fileName:inputs[4].value,
            fileDate:inputs[5].value,
            fileSex:select[1].value,
            filePhone:Number(inputs[6].value),
            clazzId:classId,
            fileStuAccount:StuAccount
        }
        console.log(Json2)
        ajax.post(ajax.myUrl+"stu/updateStu",Json2,function(result){

        })
    }
    var cancel1 = addpreStudent1.getElementsByClassName("cancel1")[0];
    cancel1.onclick = function(){
        addpreStudent1.style.display = "none";
    }
    
})

}
// 鼠标经过加号显示要添加的内容
var addition = document.getElementsByClassName("addition")[0];
//加号
var addi = addition.getElementsByClassName("addi")[0];
//要显示的内容
var add = addition.getElementsByClassName("add")[0];
addi.onmouseover = function(){
    add.style.display = "block";
}
addi.onmouseout = function(){
    add.style.display = "none";
}



//点击加号添加预招生，弹出模态框
//获取模态框
var addpreStudent = document.getElementsByClassName("addpreStudent")[0];
//加号
var addi = document.getElementsByClassName("addi")[0];
addi.onclick = function(){
    addpreStudent.style.display = "block";
}
//点击添加预招生模态框中的确定或取消，模态框消失
var sure = addpreStudent.getElementsByClassName("sure")[0];
sure.onclick = function(){
    //生成学生账户
    var date = new Date();
    var month = date.getMonth();
    if(month<9){
        month = "0"+(month+1);  //月
    }
    var day = date.getDate();
    var num = Math.floor(Math.random()*100);
    if(num<10){
        num = "0"+num;  //月
    }
    var  StuAccount = "br"+month+day+num;
    addpreStudent.style.display = "none";
    var inputs = document.querySelectorAll("input");
    // console.log(inputs);
    var select = document.querySelectorAll("select");
    // console.log(select[1].value)
    var classId = null;
   
    if(select[0].value=="JAVA班"){
        classId=1
    }else if(select[0].value=="ui"){
        classId=2
    }else if(select[0].value=="python"){
        classId=4
    }
    var Json3 = {
        fileName:inputs[1].value,
        fileSex:select[1].value,
        fileDate:inputs[2].value,
        clazzId:classId,
        fileType:0,
        fileStuAccount:"223"

    }
    console.log(Json3)
    ajax.post(ajax.myUrl+"stu/addStudent",Json3,function(result){

        })
}
var cancel = addpreStudent.getElementsByClassName("cancel")[0];
cancel.onclick = function(){
    addpreStudent.style.display = "none";
}
var btn  = document.querySelector("button");
btn.onclick = function(){
    var inputs = document.querySelectorAll("input")
    // console.log(1)
    var Json={
        pageSize:1,
        pageIndex:1,
        fileType:0,
        fileName:inputs[0].value
    }
    ajax.post(ajax.myUrl+"stu/searchStu",Json,function(result){
        var data = result.dataset.studentList;
        //    console.log(data[0])
            var tbody = document.querySelector("tbody");
            tbody.innerHTML=`
                    <td>${data[0].fileName}</td>
                    <td>${data[0].fileSex}</td>
                    <td>${data[0].clazzName}</td>
                    <td>${data[0].fileDate}</td>
                    <td>${data[0].filePhone}</td>
                    
                    <td><div class="remove">修改</div>
                    <div class="delete">删除</div></td>

            `;
            var removes = document.querySelectorAll(".remove");
            var addstudying1 = document.querySelectorAll(".addstudying1")[0];//console.log(addstudying1);
            for(var i=0;i<removes.length;i++){
                localStorage.setItem("fileIds","");
                removes[i].onclick = function(event){
                    for(var j=0;j<removes.length;j++){
                        if(this == removes[j]){
                            // console.log(addstudying1);
                            // 模态框显示
                            // addstudying1.style.display = "block";
                            // console.log(addstudying1)
                            // 获取到本条数据
                            // console.log(data[j]);
                            // 将姓名默认显示到修改页面
                            var username =  addstudying1.querySelector("input");
                            username.value=data[j].fileName;
                            // localStorage.setItem("fileNames",data[j].fileName);
                            localStorage.setItem("fileIds",data[j].fileId);
                            var date = new Date();
                            var month = date.getMonth();
                            if(month<9){
                                month = "0"+(month+1);  //月
                            }
                            var day = date.getDate();
                            var num = Math.floor(Math.random()*100);
                            if(num<10){
                                num = "0"+num;  //月
                            }
                            // //获取账号框
                            var number1 = document.getElementsByClassName("number1")[0];
                            //console.log("br"+month+day+num);
                            number1.innerHTML = "br"+month+day+num;
                           }
                        }
                }
             }
                //点击删除用户
                var deletes = document.getElementsByClassName("delete");
                // console.log(deletes);
                // deletes[0].onclick=function(){
                //     console.log(1)
                // }
                for(var j=0;j<deletes.length;j++){
                    deletes[j].onclick=function(){
                        for(var k=0;k<deletes.length;k++){
                            if(this==deletes[k]){
                                // console.log(data[k].clazzId)
                                // console.log(data[k].fileId)
                                // console.log(data[k].fileType)
                                var Json4={
                                    clazzId: data[k].clazzId,
                                    fileId: data[k].fileId,
                                    fileType:1
                                }
                                ajax.post(ajax.myUrl+"stu/deleteStu",Json4,function(result){
    
                                })
                            }
                        }
                    }
                }
       
       }) 
    }



        




